﻿@model TopicModel

@using Nop.Core
@using Nop.Core.Domain.Seo
@using Nop.Core.Domain.Topics

@inject IWebHelper webHelper
@inject SeoSettings seoSettings

@{
    var isPopup = ViewBag.IsPopup ?? false;

    if (!isPopup)
    {
        Layout = "_ColumnsOne";
    }

    if (!Model.IsPasswordProtected)
    {
        //title
        NopHtml.AddTitleParts(!string.IsNullOrEmpty(Model.MetaTitle) ? Model.MetaTitle : Model.Title);
        //meta
        NopHtml.AddMetaDescriptionParts(Model.MetaDescription);
        NopHtml.AddMetaKeywordParts(Model.MetaKeywords);
    }

    if (seoSettings.CanonicalUrlsEnabled)
    {
        var topicUrl = Url.RouteUrl<Topic>(new { SeName = Model.SeName }, webHelper.GetCurrentRequestProtocol()).ToLowerInvariant();
        NopHtml.AddCanonicalUrlParts(topicUrl, seoSettings.QueryStringInCanonicalUrlsEnabled);
    }

    //page class
    NopHtml.AppendPageCssClassParts("html-topic-page");
}
@if (Model.IsPasswordProtected)
{
    <script asp-location="Footer">
        $(function() {
            $('#button-password').on('click', function () {
                var postData = {
                    id: $("#topic-@Model.Id").val(),
                    password: $('#password').val()
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@Url.RouteUrl("TopicAuthenticate")",
                    data: postData,
                    dataType: "json",
                    success: function (data, textStatus, jqXHR) {
                        if (data.Authenticated) {
                            $('#ph-topic #ph-title h1').html(data.Title);
                            if ($('#ph-topic #ph-title h1').text().length == 0) {
                                $('#ph-title').hide();
                            }
                            $('#ph-topic .page-body').html(data.Body);
                            $('#ph-password').hide();
                            $('#ph-topic').show();
                            //we need to re-run the validation plugin after the content is loaded after successful authentication
                            $.validator.unobtrusive.parse('#ph-topic');
                        }
                        else {
                            $('#password-error').text(data.Error).fadeIn("slow");
                            $('#ph-password #password').select().focus();
                        }
                    }
                });
                return false;
            });
        });

        $(function() {
            $('#ph-topic').hide();
            $('#ph-password #password').select().focus();
        });
    </script>
    <div class="topic-password" id="ph-password">
        <form asp-route="TopicAuthenticate" method="post" autocomplete="off">
            <input asp-for="Id" id="topic-@Model.Id" type="hidden" />
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                <input name="password" id="password" type="password" />
                <button type="submit" id="button-password" class="button-1 topic-password-button">@T("Topic.Button")</button>
            </div>
            <div class="password-error" id="password-error" style="display: none;"></div>
        </form>
    </div>
}
<div class="page topic-page" id="ph-topic">
    @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.CustomerTopicDetailsTop, additionalData = Model })
    <div class="page-title" id="ph-title">
        <h1>@Model.Title</h1>
    </div>
    <div class="page-body">
        @Html.Raw(Model.Body)
    </div>
    @await Component.InvokeAsync(typeof(WidgetViewComponent), new { widgetZone = PublicWidgetZones.CustomerTopicDetailsBottom, additionalData = Model })
</div>